﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TestFileReaderAndWriter
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] test = OpenFileToByteArray(@"C:\workspace\PFM-Config.xml");
            Console.WriteLine("数组长度为："+test.Length);
            foreach (byte b in test)
                Console.Write(b);
            Console.Read();

            SaveByteArrayToFile(@"C:\workspace\Test.xml", test);
        }
       
        static byte[] OpenFileToByteArray(string fileName)
        {
            byte[] result = null;
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                result = new byte[fs.Length];
                try
                {
                    fs.Read(result, 0, (int)fs.Length);
                }
                finally
                {
                    fs.Close();
                }
            }
            return result;

        }
        static void SaveByteArrayToFile(string fileName, byte[] baTarget)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate,
                FileAccess.Write))
            {
                try
                {
                    fs.Write(baTarget, 0, (int)baTarget.Length);
                }
                finally
                {
                    fs.Close();
                }
            }
        }

    }
}
